<template>
  <view v-if="getCartList.length > 0" class="cart-container">
    <!-- 收货地址 -->
    <my-address></my-address>
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    <uni-swipe-action>
      <uni-swipe-action-item
        v-for="item in getCartList"
        :key="item.goods_id"
        :right-options="options"
        @click="delete_goods(item.goods_id)"
      >
        <my-goods :goods="item">
          <!-- 左边选中按钮 --- 插槽 -->
          <template #radio>
            <radio
              :checked="item.goods_state"
              color="#fa5151"
              @click.stop="changeState(item.goods_id, !item.goods_state)"
            ></radio>
          </template>
          <!-- 右边数字输入框 --- 插槽 -->
          <template #number>
            <view @click.stop="">
              <number>
                <uni-number-box
                  v-model="item.goods_number"
                  :min="1"
                  @change="changenumber(item.goods_id, $event)"
                />
              </number>
            </view>
          </template>
        </my-goods>
      </uni-swipe-action-item>
    </uni-swipe-action>
    <!-- 结算组件 -->
    <my-settle></my-settle>
  </view>
  <view v-else class="empty-cart">
    <image
      class="empty-img"
      src="../../static/cart_empty@2x.png"
      mode="scaleToFill"
    />
    <text class="tip-text">空空如也~</text>
  </view>
</template>

<script>
// 导入混入
import tabBar_badge from "@/mixins/tabBar-badge";
// 导入vuex辅助函数
import { mapGetters, mapMutations } from "vuex";

export default {
  mixins: [tabBar_badge],
  data() {
    return {
      options: [
        {
          text: "删除",
          style: {
            backgroundColor: "#dd524d",
          },
        },
      ],
    };
  },
  computed: {
    ...mapGetters("cart", ["getCartList"]),
  },
  methods: {
    ...mapMutations("cart", [
      "modifyGoodsstate",
      "modifyGoodsNumber",
      "delete_Goods_id",
    ]),
    // 修改商品的状态
    changeState(goods_id, goods_state) {
      // console.log(goods_id, goods_state);
      this.modifyGoodsstate({ goods_id, goods_state });
    },
    // 修改商品的数量
    changenumber(goods_id, goods_number) {
      // console.log(goods_id, goods_number);
      this.modifyGoodsNumber({ goods_id, goods_number });
    },
    // 删除商品
    delete_goods(goods_id) {
      // console.log(goods_id);
      this.delete_Goods_id(goods_id);
    },
  },
};
</script>

<style lang="scss" scoped>
.cart-container {
  padding-bottom: 50px;
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    .cart-title-text {
      margin-left: 10px;
    }
  }
}
.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
